package com.py.framework.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.py.framework.common.Constants;
import com.py.framework.core.exception.ServiceException;
import com.py.framework.dao.WorkFlowRoleDao;
import com.py.framework.entity.WorkFlowRole;
import com.py.framework.service.WorkFlowRoleService;

/**
 * 工作流角色管理Service实现
 * 
 * @author Leegern
 * @date 2018年5月23日
 */
@Service
public class WorkFlowRoleServiceImpl implements WorkFlowRoleService {

	@Autowired
	private WorkFlowRoleDao workFlowRoleDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.py.framework.service.WorkFlowRoleService#addWorkFlowRole(com.py.
	 * framework.entity.WorkFlowRole)
	 */
	@Override
	public int addWorkFlowRole(WorkFlowRole entity) {
		if (null != entity) {
			entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
			entity.setUpdateTime(new Timestamp(System.currentTimeMillis()));
			entity.setDelFlag(Constants.NUMBER_ONE);
			return workFlowRoleDao.addWorkFlowRole(entity);
		}
		return 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.py.framework.service.WorkFlowRoleService#queryWorkFlowRoleForPage(
	 * java.util.Map)
	 */
	@Override
	public List<WorkFlowRole> queryWorkFlowRoleForPage(Map<String, Object> params) {
		if (!CollectionUtils.isEmpty(params)) {
			return workFlowRoleDao.queryWorkFlowRoleForPage(params);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.py.framework.service.WorkFlowRoleService#findOneWorkFlowRole(long)
	 */
	@Override
	public WorkFlowRole findOneWorkFlowRole(long id) {
		if (id > Constants.NUMBER_ZERO) {
			return workFlowRoleDao.findOneWorkFlowRole(id);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.py.framework.service.WorkFlowRoleService#getAllWorkFlowRoles()
	 */
	@Override
	public List<WorkFlowRole> getAllWorkFlowRoles() {
		return workFlowRoleDao.getAllWorkFlowRoles();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.py.framework.service.WorkFlowRoleService#updateWorkFlowRole(com.py.
	 * framework.entity.WorkFlowRole)
	 */
	@Override
	public int updateWorkFlowRole(WorkFlowRole entity) {
		if (null != entity && entity.getId() > Constants.NUMBER_ZERO) {
			entity.setUpdateTime(new Timestamp(System.currentTimeMillis()));
			return workFlowRoleDao.updateWorkFlowRole(entity);
		}
		return 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.py.framework.service.WorkFlowRoleService#batchDelWorkFlowRole(java.
	 * util.List)
	 */
	@Override
	public int batchDelWorkFlowRole(List<Long> ids) {
		if (!CollectionUtils.isEmpty(ids)) {
			Map<String, Object> params = new HashMap<>();
			List<WorkFlowRole> roles = null;
			for (Long roleId : ids) {
				roles = workFlowRoleDao.getWFRoleUserById(roleId);
				if (!CollectionUtils.isEmpty(roles)) {
					throw new ServiceException("此工作流角色已绑定用户，删除失败！");
				}
			}
			params.put("ids", ids);
			params.put("updateTime", new Timestamp(System.currentTimeMillis()));
			return workFlowRoleDao.batchDelWorkFlowRole(params);
		}
		return 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.py.framework.service.WorkFlowRoleService#isExistWorkFlowRole(java.
	 * util.Map)
	 */
	@Override
	public List<WorkFlowRole> isExistWorkFlowRole(Map<String, Object> params) {
		if (!CollectionUtils.isEmpty(params)) {
			return workFlowRoleDao.isExistWorkFlowRole(params);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.py.framework.service.WorkFlowRoleService#getWorkFlowRoleByUserId(
	 * long)
	 */
	@Override
	public List<WorkFlowRole> getWorkFlowRoleByUserId(long userId) {
		List<WorkFlowRole> datas = null;
		if (userId > Constants.NUMBER_ZERO) {
			datas = workFlowRoleDao.getWorkFlowRoleByUserId(userId);
		}
		return datas == null ? new ArrayList<WorkFlowRole>() : datas;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.py.framework.service.WorkFlowRoleService#modifyWFRoleUser(com.py.
	 * framework.entity.WorkFlowRole)
	 */
	@Override

	public int modifyWFRoleUser(List<WorkFlowRole> entities) {
		int flag = 0;

		if (entities != null && entities.size() > 0) {
			Map<String, Object> params = new HashMap<>();
			WorkFlowRole entity = entities.get(0);
			if (null != entity
					&& (entity.getUserId() > Constants.NUMBER_ZERO || entity.getId() >= Constants.NUMBER_ZERO)) {
				params.put("userId", entity.getUserId());
				// 先删除工作流角色与用户关系
				workFlowRoleDao.delWFRoleUser(params);

			}

		}

		for (WorkFlowRole entity : entities) {

			if (null != entity
					&& (entity.getUserId() > Constants.NUMBER_ZERO || entity.getId() >= Constants.NUMBER_ZERO)) {
				Map<String, Object> params = new HashMap<>();
				if (entity.getUserId() > Constants.NUMBER_ZERO) {
					params.put("userId", entity.getUserId());
				}

				// 再添加工作流角色与用户关系
				flag = workFlowRoleDao.addWFRoleUser(entity);
			}

		}
		return flag;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.py.framework.service.WorkFlowRoleService#getUserWFRole(long)
	 */
	@Override
	public List<WorkFlowRole> getUserWFRole(long userId) {

		List<WorkFlowRole> datas = null;
		if (userId > Constants.NUMBER_ZERO) {
			datas = workFlowRoleDao.getWorkFlowRoleByUserId(userId);
			// if (!CollectionUtils.isEmpty(datas)) {
			// return datas.get(Constants.NUMBER_ZERO);
			// }
		}
		return datas == null ? new ArrayList<WorkFlowRole>() : datas;
	}
}